package com.sskj.gocoin.mine;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.ShareEntry;
import com.sskj.gocoin.utils.TimeFormatUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/04/01
 */
public class MyFriendsActivity extends BaseActivity<MyFriendsPresenter> {

    @BindView(R2.id.toolbar)
    ToolBarLayout toolBarLayout;
    @BindView(R2.id.friends_recyclerView)
    RecyclerView friendsRecyclerView;

    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout smartRefreshLayout;

    private BaseAdapter<ShareEntry.Data> adapter;

    int page = 1;

    @Override
    public MyFriendsPresenter getPresenter() {
        return new MyFriendsPresenter();
    }

    @Override
    public void initView() {
        toolBarLayout.setLeftButtonOnClickLinster(v -> finish());
        friendsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        friendsRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        adapter = new BaseAdapter<ShareEntry.Data>(R.layout.item_friend, null, friendsRecyclerView, true) {
            @Override
            public void bind(ViewHolder holder, ShareEntry.Data item) {
                holder.setText(R.id.name, TextUtils.isEmpty(item.getRealname()) ? "---" : item.getRealname())
                        .setText(R.id.account, TextUtils.isEmpty(item.getMobile()) ? item.getMail() : item.getMobile())
                        .setText(R.id.time, TimeFormatUtil.SF_FORMAT_B.format(item.getReg_time() * 1000));
            }
        };
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getData(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getData(page);
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_friends_n;
    }

    @Override
    public void initData() {
        mPresenter.getData(1);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MyFriendsActivity.class);
        context.startActivity(intent);
    }

    public void setData(ShareEntry data) {
        if (page == 1) {
            adapter.setNewData(data.getList());
        }
    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }
}
